#include <bits/stdc++.h>
using namespace std;
int ma[105][105] = {0};
int step[105][105];
int n, m;
int sh, sl, th, tl;
queue<int> Qx;
queue<int> Qy;

int di[] = {-1, 1, 0, 0};
int dj[] = {0, 0, -1, 1};

int main() {

  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      if (c == '.') {
        ma[i][j] = 1;
      } else if (c == 'T') {
        th = i;
        tl = j;
        ma[i][j] = 1;
      } else if (c == 'S') {
        sh = i;
        sl = j;
        ma[i][j] = 1;
      }
    }
  }
  Qx.push(sh);
  Qy.push(sl);
  step[sh][sl] = 0;
  while (!Qx.empty()) {
    int fi = Qx.front(); // 取队首
    int fj = Qy.front();
    Qx.pop(); // 出队
    Qy.pop();
    if (fi == th && fj == tl) {
      cout << step[fi][fj];
      break;
    }

    for (int k = 0; k < 4; k++) {
      int sonx = fi + di[k];
      int sony = fj + dj[k];
      if (ma[sonx][sony] == 1) {
        Qx.push(sonx);
        Qy.push(sony);
        step[sonx][sony] = step[fi][fj] + 1;
        ma[sonx][sony] = 0;
      }
    }
  }

  return 0;
}
